/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.cmcc.zysoft.infocollection.dao.CompanyPcDao;
import com.cmcc.zysoft.infocollection.dao.DepartmentPcDao;
import com.cmcc.zysoft.infocollection.model.Company;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: CompanyPcService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-4 下午3:03:36
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class CompanyPcService extends BaseServiceImpl<Company, String> {
	
	/**
	 * 属性名称: companyPcDao.
	 * 类       型: CompanyPcDao.
	 */
	@Resource
	private CompanyPcDao companyPcDao;
	
	/**
	 * 属性名称: departmentPcDao.
	 * 类       型: DepartmentPcDao.
	 */
	@Resource
	private DepartmentPcDao departmentPcDao;
	
	/**
	 * 属性名称: departmentPcService.
	 * 类       型: DepartmentPcService.
	 */
	@Resource
	private DepartmentPcService departmentPcService;
	
	@Override
	public HibernateBaseDao<Company, String> getHibernateBaseDao() {
		return this.companyPcDao;
	}
	
	/**
	 * 获取企业列表信息.
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> companyTree() {
		List<Map<String, Object>> companyList = this.companyPcDao.companyTree("0");
		return this.getSubComp(companyList);
	}
	
	/**
	 * 获取子企业列表信息.
	 * @param companyList
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> getSubComp(List<Map<String, Object>> companyList) {
		for(Map<String, Object> map : companyList) {
			String haveChildCompany = map.get("have_child_company").toString();
			if("Y".equals(haveChildCompany)) {
				String parentCompanyId = map.get("company_id").toString();
				List<Map<String, Object>> childList = this.companyPcDao.companyTree(parentCompanyId);
				this.getSubComp(childList);
				map.put("children", childList);
				map.put("state", "closed");
			} else {
				map.put("state", "open");
			}
		}
		return companyList;
	}
	
	/**
	 * 根据企业名称搜索企业.
	 * @param companyName 企业名称
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> search(String companyName) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = this.companyPcDao.search(companyName);
		List<Map<String, Object>> parentList = new ArrayList<Map<String, Object>>();
		if(null != list && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				map = list.get(i);
				if(!"0".equals(map.get("parent_company_id").toString())) {
					parentList.addAll(this.searchParent(map.get("parent_company_id").toString()));
				}
			}
			list.addAll(parentList);
		}
		return list;
	}
	
	/**
	 * 根据父企业ID搜索父企业信息.
	 * @param parentCompanyId 父企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> searchParent(String parentCompanyId) {
		List<Map<String, Object>> parentList = this.companyPcDao.searchCompByCompId(parentCompanyId);
		Map<String, Object> parentMap = parentList.get(0);
		if(!"0".equals(parentMap.get("parent_company_id").toString())) {
			parentList.addAll(this.searchParent(parentMap.get("parent_company_id").toString()));
		}
		return parentList;
	}
	
	/**
	 * 添加企业.
	 * @param company 企业信息
	 * @param userId 当前用户ID
	 * @return String
	 */
	public String addCompany(Company company, String userId) {
		company.setIndexLogo("");
		company.setHaveChildCompany("N");
		company.setDelFlag("0");
		company.setParentCompanyId("0");
		company.setCreateMan(userId);
		company.setCreateDate(new Date());
		String companyId = this.companyPcDao.save(company);
		if(StringUtils.hasText(companyId)) {
			return "SUCCESS";
		} else {
			return "ERROR";
		}
	}
	
	/**
	 * 更新企业信息.
	 * @param company 企业信息
	 * @param userId 当前用户ID
	 * @return String
	 */
	public String updateCompany(Company company, String userId) {
		Company comp = this.companyPcDao.get(company.getCompanyId());
		comp.setCompanyCode(company.getCompanyCode());
		comp.setCompanyName(company.getCompanyName());
		comp.setCompanyAddress(company.getCompanyAddress());
		comp.setContactMan(company.getContactMan());
		comp.setTelephone(company.getTelephone());
		comp.setFax(company.getFax());
		comp.setModifyMan(userId);
		comp.setModifyDate(new Date());
		this.companyPcDao.update(comp);
		return "SUCCESS";
	}
	
	/**
	 * 检查企业代码是否已存在.
	 * @param companyId 企业ID
	 * @param companyCode 企业代码
	 * @return Boolean(true:存在,false-不存在)
	 */
	public Boolean checkCompanyCode(String companyId, String companyCode) {
		List<Map<String, Object>> list = this.companyPcDao.checkCompanyCode(companyId, companyCode);
		if(null == list || list.isEmpty()) {
			return false;
		}
		return true;
	}
	
	/**
	 * 检查企业名称是否已存在.
	 * @param companyId 企业ID
	 * @param companyName 企业名称
	 * @return Boolean(true:存在,false-不存在)
	 */
	public Boolean checkCompanyName(String companyId, String companyName) {
		List<Map<String, Object>> list = this.companyPcDao.checkCompanyName(companyId, companyName);
		if(null == list || list.isEmpty()) {
			return false;
		}
		return true;
	}
	
	/**
	 * 根据企业名称获取企业ID.
	 * @param companyId 企业ID
	 * @param companyName 企业名称
	 * @return String
	 */
	public String getCompanyId(String companyId, String companyName) {
		List<Map<String, Object>> list = this.companyPcDao.checkCompanyName(companyId, companyName);
		if(null != list && !list.isEmpty()) {
			return list.get(0).get("company_id").toString();
		} else {
			return "";
		}
	}
	
	/**
	 * 添加企业.
	 * @param userId 当前用户ID-企业添加人
	 * @param companyName 企业名称
	 * @return String(企业ID)
	 */
	@Transactional
	public String addCompany(String userId, String companyName) {
		Company company = new Company();
		company.setParentCompanyId("0");
		company.setCompanyName(companyName);
		company.setCreateMan(userId);
		company.setCreateDate(new Date());
		company.setHaveChildCompany("N");
		company.setDelFlag("0");
		return this.companyPcDao.save(company);
	}
	
	/**
	 * 异步加载企业部门树-员工管理.
	 * @param id 可能是公司c_id,部门d_id,加前缀区分
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> companyDeptTree(String id) {
		List<Map<String, Object>> tree = null;
		if(StringUtils.hasText(id)) { //id不为空
			List<Map<String, Object>> compList = null;
			List<Map<String, Object>> deptList = null;
			String[] type_id = id.split("_");
			switch (type_id[0]) {
				case "c" : //企业
					//根据父企业ID获取其子企业
					compList = this.companyTree(type_id[1]);
					//根据父部门ID和企业ID获取子部门
					deptList = this.departmentPcService.departmentTree("0", type_id[1]);
					compList.addAll(deptList);
					tree = compList;
					break;
				case "d" : //部门
					//根据父部门ID获取子部门
					deptList = this.departmentPcService.departmentTree(type_id[1]);
					tree = deptList;
					break;
				default : break;
			}
		} else { //id为空,初始化企业部门树
			tree = this.companyTree(id);
		}
		return tree;
	}
	
	/**
	 * 根据父企业ID获取其子企业.
	 * @param id 据父企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> companyTree(String id) {
		String companyId = "";
		List<Map<String, Object>> companys = this.companyPcDao.comTree(id);
		for(Map<String, Object> map : companys) {
			companyId = map.get("id").toString();
			map.put("id", "c_" + companyId);
			map.put("iconCls", "company");
			//根据父部门ID和企业ID获取子部门
			List<Map<String, Object>> deptList = this.departmentPcDao.departmentTree("0", companyId);
			if("Y".equals(map.get("haveChildCom").toString())) {
				map.put("state", "closed");
			} else {
				if(null == deptList || deptList.isEmpty()) {
					map.put("state", "open");
				} else {
					map.put("state", "closed");
				}
			}
		}
		return companys;
	}
}